﻿using System;
using System.Xml;
using System.Drawing;
using System.Collections.Generic;

namespace Miziak_Custom_Client_Symulator
{
    class ConfigLoader
    {
        public struct Button
        {
            public Button(string name, Point pos)
            {
                this.image = name;
                this.pos = pos;
            }

            public string image;
            public Point pos;
        }

        public struct Protocol
        {
            public Protocol(long ip, long rsa, int server_count)
            {
                this.ip = ip;
                this.rsa = rsa;
                this.server_count = server_count;
            }

            public long ip;
            public long rsa;
            public int server_count;
        }

        public ConfigLoader()
        {
            XmlReader reader = XmlReader.Create("config.xml");

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && reader.Name == "button")
                {
                    if (reader.GetAttribute("type") == "start")
                    {
                        int x = int.Parse(reader.GetAttribute("x"));
                        int y = int.Parse(reader.GetAttribute("y"));

                        start = new Button(reader.GetAttribute("image"), new Point(x, y));
                    }
                    else if (reader.GetAttribute("type") == "exit")
                    {
                        int x = int.Parse(reader.GetAttribute("x"));
                        int y = int.Parse(reader.GetAttribute("y"));

                        exit = new Button(reader.GetAttribute("image"), new Point(x, y));
                    }
                }
                else if (reader.NodeType == XmlNodeType.Element && reader.Name == "background")
                    background = reader.GetAttribute("image");
                else if (reader.NodeType == XmlNodeType.Element && reader.Name == "server")
                {
                    name = reader.GetAttribute("otsName");
                    IP = reader.GetAttribute("ip");
                    port = int.Parse(reader.GetAttribute("port"));
                    protocol = reader.GetAttribute("protocol");
                }
            }

            reader.Close();

            reader = XmlReader.Create("versions.xml");

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && reader.Name == "version")
                {
                    long ip = Convert.ToInt32(reader.GetAttribute("loginServerStart"), 16);
                    long rsa = Convert.ToInt32(reader.GetAttribute("rsaKeyAdress"), 16);
                    string v = reader.GetAttribute("protocol");
                    int count = 10;

                    if (v == "7.60" || v == "7.80" || v == "7.92")
                        count = 5;

                    versions.Add(v, new Protocol(ip, rsa, count));
                }
            }

            reader.Close();
        }

        public string background;
        public Button start;
        public Button exit;
        public string name;
        public string IP;
        public int port;
        public string protocol;
        public Dictionary<string, Protocol> versions = new Dictionary<string, Protocol>();
    }
}
